<!--  -->
<template>
  <a-modal :visible="visible" title="受试者信息" :footer="null" @cancel="handleCancel" :width="1000">
    <a-descriptions title="基本信息" bordered size="small">
      <a-descriptions-item label="受试者ID"> {{ PtsDetailsList.ptsNo }} </a-descriptions-item>
      <a-descriptions-item v-if="$options.filters.isPtsVisible()" label="姓名"> {{ PtsDetailsList.ptsName }} </a-descriptions-item>
      <a-descriptions-item v-if="$options.filters.isPtsVisible()" label="电话/手机"> {{ PtsDetailsList.ptsPhone }} </a-descriptions-item>
      <a-descriptions-item v-if="$options.filters.isPtsVisible()" label="身份证号码"> {{ PtsDetailsList.ptsIdentity }} </a-descriptions-item>
      <a-descriptions-item label="性别"> {{ PtsDetailsList.ptsGender | filterGender }} </a-descriptions-item>
      <a-descriptions-item label="年龄"> {{ PtsDetailsList.year }} </a-descriptions-item>
      <a-descriptions-item label="所属医院"> {{ PtsDetailsList.orgName }} </a-descriptions-item>
      <!-- <a-descriptions-item label="所属科室"> {{ PtsDetailsList.ptsNo }} </a-descriptions-item> -->
      <a-descriptions-item label="所属CRC"> {{ PtsDetailsList.inviteUserNo }} </a-descriptions-item>
      <a-descriptions-item label="中心负责人"> {{ PtsDetailsList.principalName }} </a-descriptions-item>
      <a-descriptions-item v-if="!isEdit" label="当前状态"> {{ form.status | enumValueByKey($const.PTS_STATUS) }} </a-descriptions-item>
      <a-descriptions-item v-if="!isEdit" label="研究人员"> {{ researchName }} </a-descriptions-item>
      <a-descriptions-item v-if="PtsDetailsList.refuseReason" label="审批意见"> {{ PtsDetailsList.refuseReason }} </a-descriptions-item>
    </a-descriptions>
    <!-- 编辑时 -->
    <div v-if="isEdit" class="table-page-search-wrappers">
      <!-- 筛选区域 -->
      <a-form layout="inline" v-if="thisStatus == 1" style="margin-top: 40px;">
        <a-row :gutter="48">
          <a-col :md="10" :sm="24">
            <a-form-item label="当前状态">
              <a-select v-model="form.status">
                <a-select-option
                  v-for="item in statusList"
                  :value="item.id"
                  :key="item.id"
                  @click="switchitem(item, 'status')"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="10" :sm="24">
            <a-form-item label="研究人员">
              <a-select v-model="form.reId">
                <a-select-option
                  v-for="item in theDoctorList"
                  :value="item.id"
                  :key="item.id"
                  @click="switchitem(item, 'reId')"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-descriptions title="费用管理" style="margin-top:20px;" v-if="thisStatus == 1" />
    <VisitTable ref="visitRef" :subjectId="subjectId" v-if="thisStatus == 1 && visible"></VisitTable>
    <a-descriptions v-if="thisStatus == 3 && ($auth('PROJECT.PROJECT_PATIENT_EDIT') || this.auditPmsVisible )" title="小程序管理审核" style="margin-top:20px;" />
    <div v-if="thisStatus == 3 && ($auth('PROJECT.PROJECT_PATIENT_EDIT') || this.auditPmsVisible )" >
      <div style="width:40%;margin-bottom:15px">
        <a-input v-model="refuseReason" type="textarea" placeholder="审批意见" />
      </div>
      <a-button type="primary" @click="agreement(1)" style="margin-right: 30px;">同意</a-button>
      <a-button type="danger" @click="agreement(2)">拒绝</a-button>
    </div>
    <!-- <div style="margin-top:20px" v-if="thisStatus == 5 && $auth('PROJECT.PROJECT_PATIENT_EDIT')" >
      <a-button type="primary" @click="clickReinvite" >邀请</a-button>
    </div> -->
  </a-modal>
</template>

<script>
import monitor from '@/utils/monitor'
import viewModal from '@/monitor/views/overview/detail/responsible/components/subject/viewModal.js'

import VisitTable from './visitTable.vue'
import { PtsDetails, doctorList, upDatePtsStatus, updatePtsReId, updatePtsAuth, reinviteSubject } from '@/api/overview/subject'
import { getInfo } from '@/api/login'
const watch = monitor(viewModal)

export default {
  name: 'ViewModal',
  components: {
    VisitTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    uId: {
      type: [Number, String],
      default: null
    },
    mProId: {
      type: [Number, String],
      default: null
    },
    // thisStatus: {
    //   type: [Number, String],
    //   default: null
    // },
    isEdit: {
      type: [Number, Boolean],
      default: 0
    }
  },
  data () {
    return {
      thisStatus: '',
      refuseReason: '',
      infoObj: {},
      form: {
        status: null,
        reId: null
      },
      proId: '',
      ptsNo: '',
      reId: '',
      statusList: [
        {
          id: 1,
          name: '待筛选'
        },
        {
          id: 2,
          name: '筛选中'
        },
        {
          id: 3,
          name: '筛选失败'
        },
        {
          id: 4,
          name: '随机'
        },
        {
          id: 5,
          name: '治疗中'
        },
        {
          id: 6,
          name: '退出'
        },
        {
          id: 7,
          name: '治疗结束'
        },
        {
          id: 8,
          name: '安全性随访'
        },
        {
          id: 9,
          name: '生存随访'
        }
      ],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 }
      },
      PtsDetailsList: {}, // 受试者详细信息
      theDoctorList: [], // 显示当前项目研究人员
      subjectId: null, // 当前受试者id
      researchName: '', // 研究人员姓名
      auditPmsVisible: false // 审核人员显示
    }
  },
  filters: {
    filterGender (val) {
      if (val === -1) {
        return '未知'
      } else if (val === 0) {
        return '女'
      } else if (val === 1) {
        return '男'
      }
    }
  },
  watch: {
    visible (newValue) {
      if (newValue) {
        this.getPtsDetails(this.uId)
      } else {
        this.thisStatus = ''
      }
      if (this.mProId) {
        this.proId = this.mProId
        // 查询项目权限
        getInfo({ proId: this.proId }).then(res => {
          this.auditPmsVisible = !!(res.pms && res.pms.indexOf('PROJECT_PATIENT_EDIT') >= 0)
        })
      } else {
        this.proId = this.$route.query.id
      }
    }
  },
  mounted () {

  },

  methods: watch({
    // 邀请
    clickReinvite () {
      if (this.PtsDetailsList) {
        reinviteSubject({ id: this.PtsDetailsList.id,
        ptsNo: this.PtsDetailsList.ptsNo,
        proId: this.proId,
        status: this.PtsDetailsList.authStatus })
        .then(res => {
          this.$message.success('邀请成功')
          this.getPtsDetails(this.uId)
        })
        .catch(() => {
          this.$message.error('邀请失败')
        })
      }
    },
    agreement (value) {
      if (value === 2 && !this.refuseReason) {
        this.$message.error('请填写拒绝理由')
        return
      }
      this.approvalSubject()
      this.spUpdatePtsAuth({ authStatus: value })
    },
    // 受试者审批
    spUpdatePtsAuth (parameter) {
      updatePtsAuth({ ...parameter, id: this.uId, refuseReason: this.refuseReason })
      .then(res => {
        this.$message.success('审核完成')
        this.handleCancel()
      })
      .catch(() => {})
    },
    switchitem (item, value) {
      console.log(item, value)
      if (value === 'status') {
        this.changeStatus()
        this.form.status = item.id
        this.switchUpDatePtsStatus({ status: item.id })
      }
      if (value === 'reId') {
        this.changePerson()
        this.switchUpdatePtsReId({ reId: item.id })
      }
    },
    // 修改当前状态
    switchUpDatePtsStatus (parameter) {
      upDatePtsStatus({ ...parameter, proId: this.proId, ptsNo: this.ptsNo })
        .then(res => {
          console.log(res)
          this.$message.success('状态修改成功')
        })
        .catch(() => {
          this.$message.error('状态修改失败')
        })
    },
    // 研究人员修改或添加
    switchUpdatePtsReId (parameter) {
      updatePtsReId({ ...parameter, proId: this.proId, ptsNo: this.ptsNo })
        .then(res => {
          console.log(res)
          this.$message.success('研究人员修改成功')
        })
        .catch(() => {
          this.$message.error('研究人员修改失败')
        })
    },
    // 请求当前项目研究人员列表
    getDoctorList () {
      doctorList({ researchId: this.PtsDetailsList.researchId, proId: this.proId })
        .then(res => {
          console.log(res)
          console.log(this.form.reId)
          this.theDoctorList = res
          // 匹配研究人员姓名 将其展示
          if (res.length > 0) {
            this.researchName = res.find(item => item.id === this.form.reId).name
            console.log(this.researchName, '研究人员姓名')
          }
        })
        .catch(() => {})
    },
    // 请求受试者详细信息
    getPtsDetails (value) {
      this.subjectId = value
      PtsDetails({ id: value })
        .then(res => {
          this.PtsDetailsList = res
          this.thisStatus = res.authStatus.toString()
          this.form.status = res.ptsStatus
          this.form.reId = res.researchDetailId
          this.ptsNo = res.ptsNo
          console.log(res, '获取的受试者详情')
          this.getDoctorList()
        })
        .catch(() => {})
    },
    // 状态回到
    handChange () {
      this.$message.success('操作成功')
    },
    // 关闭回调
    handleCancel () {
      this.refuseReason = ''
      this.researchName = ''
      this.form = { status: null, reId: null }
      this.PtsDetailsList = {}
      // this.theDoctorList = []
      //   this.$message.success('操作成功')
      this.$emit('closeViewModal')
    },
    // 匹配研究人员姓名
    // matchingResearchName () {
    //   const target = this.theDoctorList.find(item => item.id === this.form.reId)
    //   this.researchName = target.name
    // },
    // 受试者审核埋点
    approvalSubject () {},
    // 修改受试者状态埋点
    changeStatus () {},
    // 修改研究人员埋点
    changePerson () {}
  })
}
</script>
<style scoped lang="scss"></style>
